package com.zhengqiangblog.blog.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.zhengqiangblog.blog.cache.ArticleTagCache;
import com.zhengqiangblog.blog.cache.TagCache;
import com.zhengqiangblog.blog.model.ArticleTag;
import com.zhengqiangblog.blog.model.HotTag;
import com.zhengqiangblog.blog.model.Tag;
import com.zhengqiangblog.core.ApplicationContext;

public class ArticleTagService {

	private static final Logger logger = Logger.getLogger(ArticleTagService.class);

	private ArticleTagCache articleTagCache = ApplicationContext.get(ArticleTagCache.class);
	private TagCache tagCache = ApplicationContext.get(TagCache.class);

	public ArticleTagService() {
		logger.info("ArticleTagService initializing...");
	}

	public int saveArticleTag(ArticleTag articleTag) throws Exception {
		return articleTagCache.saveArticleTag(articleTag);
	}

	public List<ArticleTag> getArticleTagByArticle(int articleId) throws Exception {
		return articleTagCache.getArticleTagByArticle(articleId);
	}

	public List<Integer> getArticleIdListByTagId(int tagId) throws Exception {
		return articleTagCache.getArticleIdListByTagId(tagId);
	}

	public List<HotTag> getHotTags(int size) throws Exception {
		List<HotTag> hotTags = articleTagCache.getHotTags(size);
		for (int i = 0; i < hotTags.size(); i++) {
			HotTag hotTag = hotTags.get(i);
			Tag tag = tagCache.getTag(hotTag.getTagId());
			hotTag.setTagName(tag.getName());
		}
		return hotTags;
	}

	public int deleteArticleTag(int articleId, int tagId) throws Exception {
		return articleTagCache.deleteArticleTag(articleId, tagId);
	}

	public int deleteArticleTagByArticle(int articleId) throws Exception {
		return articleTagCache.deleteArticleTagByArticle(articleId);
	}

}
